package com.kg.module.jtnc.jsydagl.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kg.core.annotation.AutoOperateLog;
import com.kg.core.annotation.NoRepeatSubmit;
import com.kg.core.exception.BaseException;
import com.kg.core.web.ResponseResult;
import com.kg.module.jtnc.jsydagl.dto.KrJsydaglDTO;
import com.kg.module.jtnc.jsydagl.dto.convert.KrJsydaglConvert;
import com.kg.module.jtnc.jsydagl.entity.KrJsydagl;
import com.kg.module.jtnc.jsydagl.service.KrJsydaglService;
import com.kg.module.jtnc.rbgl.entity.KrRbgl;
import com.kg.module.jtnc.rbgl.service.KrRbglService;
import com.kg.module.jtnc.rghs.dto.KrRghsDTO;
import com.kg.module.jtnc.rghs.entity.KrRghs;
import com.kg.module.jtnc.rghs.service.KrRghsService;
import com.kg.module.jtnc.rwgl.dto.KrRwglDTO;
import com.kg.module.jtnc.rwgl.entity.KrRwgl;
import com.kg.module.jtnc.rwgl.service.KrRwglService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 技术员档案管理表 前端控制器
 * </p>
 *
 * @author xxx
 * @since 2024-11-02
 */
@RestController
@RequestMapping("/jtnc/jsydagl/krJsydagl")
@Api(tags = "技术员档案管理表", value = "技术员档案管理表", description = "KrJsydaglController")
public class KrJsydaglController {

    @Resource
    private KrJsydaglService krJsydaglService;
    @Resource
    private KrJsydaglConvert krJsydaglConvert;

    @Resource
    private KrRbglService krRbglService;

    @Resource
    private KrRwglService krRwglService;

    @Resource
    private KrRghsService krRghsService;

    @ApiOperation(notes = "/jtnc/jsydagl/krJsydagl/listRbjl", value = "日报记录", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jsyId", value = "技术员ID", paramType = "query", required = true, dataType = "String"),
            @ApiImplicitParam(name = "year", value = "年份", paramType = "query", required = true, dataType = "String"),
            @ApiImplicitParam(name = "month", value = "月份", paramType = "query", required = false, dataType = "Integer"),
    })
    @GetMapping("/listRbjl")
    public List<String> listRbjl(@RequestParam String jsyId, @RequestParam String year, @RequestParam(required = false) Integer month) {
        List<String> result = new ArrayList<>();
        if (month == null) {
            month = DateUtil.thisMonth();
        }
        String M = month < 10 ? "0" + month : String.valueOf(month);
        String time = year + "-" + M;
        List<KrRbgl> listRb = krRbglService.lambdaQuery().likeRight(KrRbgl::getRbsj, time).eq(KrRbgl::getJsyId, jsyId).list();
        DateTime dateTime1 = DateUtil.beginOfMonth(DateUtil.parse(time + "-01"));
        DateTime dateTime2 = DateUtil.endOfMonth(DateUtil.parse(time + "-01"));
        //月初循环到月末
        while (dateTime1.isBefore(dateTime2)) {
            String day = dateTime1.toString("yyyy-MM-dd");
            long count = listRb.stream().filter(f -> f.getRbsj().equals(day)).count();
            if (count == 0) {
                result.add(day);
            }
        }
        return result;
    }
    @ApiOperation(notes = "/jtnc/jsydagl/krJsydagl/getRbxq", value = "日报详情", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jsyId", value = "技术员ID", paramType = "query", required = true, dataType = "String"),
            @ApiImplicitParam(name = "rbsj", value = "日报时间", paramType = "query", required = true, dataType = "String"),
    })
    @GetMapping("/getRbxq")
    public KrRbgl getRbxq(@RequestParam String jsyId, @RequestParam String rbsj) {
        KrRbgl krRbgl = krRbglService.lambdaQuery().eq(KrRbgl::getRbsj, rbsj).eq(KrRbgl::getJsyId, jsyId).last("limit 1").one();
        return krRbgl;
    }

    @ApiOperation(notes = "/jtnc/jsydagl/krJsydagl/getRwById", value = "任务详情", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rwId", value = "ID", paramType = "query", required = true, dataType = "String")
    })
    @GetMapping("/getRwById")
    public KrRwgl getRwById(String rwId) {
        return krRwglService.getById(rwId);
    }

    @ApiOperation(notes = "/jtnc/jsydagl/krJsydagl/listRw", value = "分页列表-任务管理", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "每页条数", paramType = "query", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "params", value = "查询条件[技术员ID jsyId 年份 year]", paramType = "query", required = false, dataType = "String")
    })
    @GetMapping("/listRw")
    public Page<KrRwglDTO> listRw(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit,
                                @RequestParam(value = "params", required = false) String params) {
        return krRwglService.pagelist(page, limit, params);
    }


    @ApiOperation(notes = "/jtnc/jsydagl/krJsydagl/getRghsById", value = "人工核算详情-", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "rghsId", value = "ID", paramType = "query", required = true, dataType = "String")
    })
    @GetMapping("/getRghsById")
    public KrRghs getRghsById(String rghsId) {
        return krRghsService.getById(rghsId);
    }

    @ApiOperation(notes = "/jtnc/jsydagl/krJsydagl/listRghs", value = "人工核算分页列表-", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "每页条数", paramType = "query", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "params", value = "查询条件[技术员ID jsyId 年份 year]", paramType = "query", required = false, dataType = "String")
    })
    @GetMapping("/listRghs")
    public Page<KrRghsDTO> listRghs(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit,
                                @RequestParam(value = "params", required = false) String params) {
        return krRghsService.pagelist(page, limit, params);
    }



    @ApiOperation(notes = "/jtnc/jsydagl/krJsydagl/select", value = "技术员下拉", httpMethod = "GET")
    @GetMapping("/select")
    public List<KrJsydagl> select() {
        List<KrJsydagl> list = krJsydaglService.lambdaQuery().orderByDesc(KrJsydagl::getCreateTime).list();
        return list;
    }

    @ApiOperation(notes = "/jtnc/jsydagl/krJsydagl/getById", value = "详情-技术员档案管理表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jsyId", value = "ID", paramType = "query", required = true, dataType = "String")
    })
    @GetMapping("/getById")
    @PreAuthorize("hasAuthority('jtnc:jsydagl:krJsydagl:getById')")
    public KrJsydaglDTO getById(String jsyId) {
        return krJsydaglConvert.entityToDto(krJsydaglService.getById(jsyId));
    }

    @ApiOperation(notes = "/jtnc/jsydagl/krJsydagl/list", value = "分页列表-技术员档案管理表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "每页条数", paramType = "query", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "params", value = "查询条件", paramType = "query", required = false, dataType = "String")
    })
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('jtnc:jsydagl:krJsydagl:list')")
    public Page<KrJsydaglDTO> list(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                   @RequestParam(value = "limit", required = false, defaultValue = "10") Integer limit,
                                   @RequestParam(value = "params", required = false) String params) {
        return krJsydaglService.pagelist(page, limit, params);
    }

    @ApiOperation(notes = "/jtnc/jsydagl/krJsydagl/add", value = "新增-技术员档案管理表", httpMethod = "POST")
    @ApiImplicitParams({})
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('jtnc:jsydagl:krJsydagl:add')")
    @NoRepeatSubmit
    @AutoOperateLog(logMethod = "/jtnc/jsydagl/krJsydagl/add", logMsg = "新增-技术员档案管理表")
    public void add(@RequestBody KrJsydaglDTO krJsydaglDTO) throws BaseException {
        try {
            krJsydaglService.add(krJsydaglDTO);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage() != null ? e.getMessage() : "新增失败！请重试");
        }
    }

    @ApiOperation(notes = "/jtnc/jsydagl/krJsydagl/update", value = "修改-技术员档案管理表", httpMethod = "POST")
    @ApiImplicitParams({})
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('jtnc:jsydagl:krJsydagl:update')")
    @NoRepeatSubmit
    @AutoOperateLog(logMethod = "/jtnc/jsydagl/krJsydagl/update", logMsg = "修改-技术员档案管理表")
    public void update(@RequestBody KrJsydaglDTO krJsydaglDTO) throws BaseException {
        try {
            krJsydaglService.update(krJsydaglDTO);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage() != null ? e.getMessage() : "修改失败！请重试");
        }
    }

    @ApiOperation(notes = "/jtnc/jsydagl/krJsydagl/delete", value = "删除-技术员档案管理表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jsyIds", value = "待删除id列表", paramType = "body", required = true, allowMultiple = true, dataType = "String")
    })
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('jtnc:jsydagl:krJsydagl:delete')")
    @NoRepeatSubmit
    @AutoOperateLog(logMethod = "/jtnc/jsydagl/krJsydagl/delete", logMsg = "删除-技术员档案管理表")
    public void delete(@RequestBody String[] jsyIds) throws BaseException {
        try {
            krJsydaglService.delete(Arrays.asList(jsyIds));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage() != null ? e.getMessage() : "删除失败！请重试");
        }
    }

    @ApiOperation(notes = "/jtnc/jsydagl/krJsydagl/export/excel", value = "导出excel-技术员档案管理表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "params", value = "查询条件", paramType = "query", required = false, dataType = "String")
    })
    @GetMapping("/export/excel")
    @PreAuthorize("hasAuthority('jtnc:jsydagl:krJsydagl:export:excel')")
    public String exportExcel(@RequestParam(value = "params", required = false) String params) throws BaseException {
        String result = krJsydaglService.exportExcel(params);
        if ("error".equals(result)) {
            throw new BaseException("导出Excel失败，请重试！");
        }
        return result;
    }

    @ApiOperation(notes = "/jtnc/jsydagl/krJsydagl/import/excel", value = "导入excel-技术员档案管理表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "request", value = "请求", paramType = "query", required = false, dataType = "HttpServletRequest")
    })
    @PostMapping("/import/excel")
    @PreAuthorize("hasAuthority('jtnc:jsydagl:krJsydagl:import:excel')")
    @NoRepeatSubmit
    @AutoOperateLog(logMethod = "/jtnc/jsydagl/krJsydagl/import/excel", logMsg = "导入excel-技术员档案管理表")
    public ResponseResult importExcel(HttpServletRequest request) throws BaseException {
        try {
            String result = krJsydaglService.importExcel(request);
            if (StringUtils.hasText(result)) {
                // 导入失败，返回错误提示信息
                return ResponseResult.builder().code("200").message(result).build();
            } else {
                return ResponseResult.success();
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(e.getMessage() != null ? e.getMessage() : "导入Excel失败，请重试！");
        }
    }

    @ApiOperation(notes = "/jtnc/jsydagl/krJsydagl/import/downloadTemplate", value = "下载导入模板-技术员档案管理表", httpMethod = "GET")
    @GetMapping("/import/downloadTemplate")
    public String downloadTemplate() throws BaseException {
        String result = krJsydaglService.downloadTemplate();
        if ("error".equals(result)) {
            throw new BaseException("下载导入模板失败，请重试！");
        }
        return result;
    }
}
